package mn.more.wits.server.model;

import java.io.Serializable;
import java.util.Date;
import mn.more.wits.client.dto.UserDTO;

/**
 * @author $Author: mikeliucc $
 * @version $Id: WitsUser.java 5 2008-09-01 12:08:42Z mikeliucc $
 */
public abstract class WitsUser implements Serializable {

	private String loginId;
	private String displayName;
	private long loginTime;
	private String mainRoleName;
	private String[] otherRoleNames;
	private String loginIp;
	private String clientBrowser;
	private AccountStatus status = AccountStatus.ACTIVE;

	public String getLoginId() { return loginId; }

	public void setLoginId(String loginId) { this.loginId = loginId; }

	public String getDisplayName() { return displayName; }

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public long getLoginTime() { return loginTime; }

	public void setLoginTime(long loginTime) { this.loginTime = loginTime; }

	public String getLoginIp() { return loginIp; }

	public void setLoginIp(String loginIp) { this.loginIp = loginIp; }

	public String getClientBrowser() { return clientBrowser; }

	public void setClientBrowser(String clientBrowser) {
		this.clientBrowser = clientBrowser;
	}

	public String getMainRoleName() { return mainRoleName; }

	public void setMainRoleName(String mainRoleName) {
		this.mainRoleName = mainRoleName;
	}

	public String[] getOtherRoleNames() { return otherRoleNames; }

	public void setOtherRoleNames(String[] otherRoleNames) {
		this.otherRoleNames =
			otherRoleNames;
	}

	public AccountStatus getStatus() { return status; }

	public void setStatus(AccountStatus status) { this.status = status; }

	/**
	 * verified that this <code>role</code> instance is a valid one.  Since this
	 * object ONLY resides on the server-side, certain database call can be issued
	 * from this method to make certain test taker's validity.
	 * <p/>
	 * Note that <code>role</code> instance is valid even without any association
	 * with a {@link ExamSession} or any {@link ExamCreator}. For example, a
	 * student may have logged in but still have not selected any test to
	 * participate.
	 */
	public boolean isValid() {
		return loginId != null &&
		       displayName != null &&
		       loginTime > System.currentTimeMillis() &&
		       mainRoleName != null &&
		       loginIp != null &&
		       clientBrowser != null;
	}

	public UserDTO toUserModel() {
		UserDTO model = new UserDTO();
		model.setUserName(loginId);
		model.setDisplayName(displayName);
		model.setLoginDate(new Date(loginTime));
		model.setMainRole(mainRoleName);
		// todo: more here?

		return model;
	}
}
